# test_client_updated.py
import asyncio
import aiohttp
import requests
import json


async def test_endpoints():
    """测试所有端点"""
    base_url = "http://localhost:9000"

    print("测试A2A服务器端点...")

    async with aiohttp.ClientSession() as session:
        # 测试根端点
        print("\n1. 测试 GET /")
        async with session.get(f"{base_url}/") as response:
            print(f"状态: {response.status}")
            text = await response.text()
            print(f"响应: {text}")

        # 测试执行端点
        print("\n2. 测试 POST /v1/execute")
        payload = {
            "input": {"text": "hello"},
            "session": {"id": "test-session-1"}
        }
        async with session.post(f"{base_url}/v1/execute", json=payload) as response:
            print(f"状态: {response.status}")
            text = await response.text()
            print(f"响应: {text}")

        # 测试带名字的问候
        print("\n3. 测试个性化问候")
        payload = {
            "input": {"text": "hello Alice"},
            "session": {"id": "test-session-2"}
        }
        async with session.post(f"{base_url}/v1/execute", json=payload) as response:
            print(f"状态: {response.status}")
            text = await response.text()
            print(f"响应: {text}")


def sync_test():
    """同步测试"""
    base_url = "http://localhost:9000"

    print("\n同步测试...")

    # 测试根端点
    response = requests.get(f"{base_url}/")
    print(f"GET / -> 状态: {response.status_code}")
    print(f"响应: {response.json()}")

    # 测试执行端点
    payload = {
        "input": {"text": "hello from sync client"},
        "session": {"id": "sync-session"}
    }
    response = requests.post(f"{base_url}", json=payload)
    print(f"POST /v1/execute -> 状态: {response.status_code}")
    print(f"响应: {response.json()}")


if __name__ == "__main__":
    print("启动A2A客户端测试")
    print("=" * 50)

    # 运行异步测试
    asyncio.run(test_endpoints())

    # 运行同步测试
    sync_test()